<?php

declare(strict_types=1);

namespace App\Controller;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Service\FlyShopService;
use App\Service\SeatsOrderService;
use Psr\Http\Message\ResponseInterface;
use Hyperf\Di\Annotation\Inject;

/**
 * 美食、订餐、订座
 * Class FoodController
 * @package App\Controller
 */
class FoodController extends AbstractController
{

    /**
     * @Inject
     * @var SeatsOrderService
     */
    private $seat;

    /**
     * 订座确认
     * @return ResponseInterface
     */
    public function seatConfirm(): ResponseInterface
    {
        return $this->jsonResponse->success($this->seat->seatVerify((int)$this->getAdminId()), 'success!');
    }

    /**
     * 生成订座订单
     * @return ResponseInterface
     */
    public function seatCreate(): ResponseInterface
    {
        $data = $this->seat->seatVerify((int)$this->getAdminId(), true);
        return $this->jsonResponse->success(['pay_order_sn' => $this->seat->serverCreate((int)$this->getAdminId(), $data, SeatsOrderService::FOOD_TYPE_SEAT)]);
    }

    /**
     * 点餐确认
     * @return ResponseInterface
     */
    public function orderConfirm(): ResponseInterface
    {
        $shop_id = (int)$this->request->input('shop_id', 0);
        return $this->jsonResponse->success($this->seat->orderVerify((int)$this->getAdminId(), $shop_id), 'success');
    }

    /**
     * 结算生成点餐订单
     * @return ResponseInterface
     */
    public function orderCreate(): ResponseInterface
    {
        $shop_id = (int)$this->request->input('shop_id', 0);
        $params = $this->seat->orderVerify((int)$this->getAdminId(), $shop_id, true);
        return $this->jsonResponse->success(['pay_order_sn' => $this->seat->serverCreate((int)$this->getAdminId(), $params, SeatsOrderService::FOOD_TYPE_ORDER)]);
    }

    /**
     * 美食订单核销
     * @return ResponseInterface
     */
    public function serviceOrderUse(): ResponseInterface
    {
        $user_id = (int)$this->getAdminId();
        if (empty($user_id)) {
            return $this->jsonResponse->success([], '未登录，请先登录');
        }
        $re = $this->seat->serviceUse($user_id);
        if (!$re) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '核销失败');
        }
        return $this->jsonResponse->success([], 'success');
    }


    /**
     * 店铺点餐评论
     * @return ResponseInterface
     */
    public function review(): ResponseInterface
    {
        $user_id = (int)$this->getAdminId();
        if (!$this->seat->review($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '评论失败');
        }
        return $this->jsonResponse->success([]);
    }

    /**
     * 获取店铺点餐评论
     * @return ResponseInterface
     */
    public function reviewList(): ResponseInterface
    {
        $user_id = (int)$this->getAdminId();
        if (empty($user_id)) {
            return $this->jsonResponse->success([], '未登录');
        }
        $id = (int)$this->request->input('id', 0);
        $style = (int)$this->request->input('style', 2);// 1店铺评论 2商品评论
        $list = $this->paginate($this->seat->reviewList($user_id, $id, $style))->toArray();
        foreach ($list['data'] as &$item) {
            $item['avatar'] = $item['avatar'] ?? "";
        }
        return $this->jsonResponse->success($list, 'success');
    }
}